<?xml version="1.0" encoding="UTF-8"?>
<xsl:stylesheet version="2.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
  xmlns:doc="livcos.org/ns/doc"
  xmlns:vz="livcos.org/ns/doc/visualization"
  xmlns:c="livcos.org/ns/cosmos"
  xmlns:f="livcos.org/ns/xslt/function"
  xmlns="http://www.w3.org/1999/xhtml"
  xmlns:xhtml="http://www.w3.org/1999/xhtml"
  xmlns:pm="livcos.org/ns/pm"
  xmlns:v="livcos.org/ns/view"
  xmlns:xs="http://www.w3.org/2001/XMLSchema"
  exclude-result-prefixes="v doc pm vz c f xhtml xs">
	
	<xsl:import href="/livcos.org/common/base.xsl"/>
	
	<xsl:template match="vz:timeline">
		<xsl:variable name="data" as="element()">
			<xsl:apply-templates select="vz:data"/>
		</xsl:variable>
		<div id="{@id}" class="block" style="overflow: auto">
			<div style="display: inline-block;">
				<v:style ref="{resolve-uri('styles.css')}"/>
				<xsl:if test="normalize-space(vz:title) != ''">
					<div class="viz_table_title">
						<xsl:copy-of select="vz:title/node()"/>
					</div>
				</xsl:if>
				<xsl:apply-templates select="$data" mode="timeln_content">
					<xsl:with-param name="timeln" select="."/>
				</xsl:apply-templates>
				<xsl:if test="$data/@time or vz:note">
					<div class="viz_table_note">
						<xsl:value-of select="vz:note"/>
						<xsl:apply-templates select="$data" mode="data_info"/>
					</div>
				</xsl:if>
			</div>
		</div>
	</xsl:template>
	
	<xsl:template match="data" mode="data_info">
		<xsl:if test="@time">
			<xsl:variable name="tStr" select="format-dateTime(@time, '[D01].[M01].[Y0001] [H01]:[m01]:[s01]')"/>
				<xsl:choose>
					<xsl:when test="@refresh">
						<span class="viz_table_refresh"	title="Data fetched at {$tStr}. Click to refresh."
							onclick="refresh('{@refresh}', {{ref: '{@ref}'}})">
							<xsl:value-of select="$tStr"/>
						</span>
					</xsl:when>
					<xsl:otherwise>
						<xsl:value-of select="$tStr"/>
					</xsl:otherwise>
				</xsl:choose>
		</xsl:if>
	</xsl:template>
	
	<xsl:template match="data" mode="timeln_content">
		<xsl:param name="timeln"/>
		<xsl:variable name="groups" as="element()*">
			<xsl:apply-templates select="$timeln/vz:groups" mode="timeln_content">
				<xsl:with-param name="data" select="."/>
			</xsl:apply-templates>
		</xsl:variable>
		<xsl:variable name="startDates" select="$groups/*/@start" as="xs:string*"/>
		<xsl:variable name="endDates" select="$groups/*/@end" as="xs:string*"/>
		<xsl:variable name="first" select="xs:date(min($startDates))"/>
		<xsl:variable name="last" select="xs:date(max($endDates))"/>
		<xsl:variable name="months" as="xs:date*">
			<xsl:variable name="monthsToFirst" select="month-from-date($first) + (year-from-date($first) * 12)"/>
			<xsl:variable name="monthsToLast" select="month-from-date($last) + (year-from-date($last) * 12)"/>
			<xsl:for-each select="0 to ($monthsToLast - $monthsToFirst)">
				<xsl:sequence select="$first + (xs:yearMonthDuration('P1M') * xs:double(.))"/>
			</xsl:for-each>
		</xsl:variable>
		<xsl:variable name="days" select="days-from-duration($last - $first)"/>
		<div id="{@ref}" class="timeline">
			<table cellspacing="0">
				<thead>
					<tr>
						<td>&#160;</td>
						<td>
							<div style="width: {ceiling(count($months) * 30.5)}px">
								<xsl:for-each select="$months">
									<xsl:variable name="d" select="days-from-duration((. + xs:yearMonthDuration('P1M')) - .)"/>
									<div style="width:{$d}px; float:left;">
										<div>
											<xsl:if test="(position() = 1) or (month-from-date(.) = 1)">
												<xsl:attribute name="class">month</xsl:attribute>
												<xsl:value-of select="format-number(year-from-date(.), '0000')"/>
											</xsl:if>
											<xsl:text>&#160;</xsl:text>
										</div>
										<div class="month">
											<xsl:value-of select="format-number(month-from-date(.), '00')"/>
										</div>
									</div>
								</xsl:for-each>
							</div>
						</td>
					</tr>
				</thead>
				<xsl:apply-templates select="$groups" mode="viz_timeln">
					<xsl:with-param name="first" select="$first" tunnel="yes"/>
				</xsl:apply-templates>
			</table>
		</div>
	</xsl:template>
	
	<xsl:template match="vz:groups" mode="timeln_content">
		<xsl:param name="data"/>
		<xsl:variable name="def" select="."/>
		<xsl:variable name="nc" select="if (@ns) then f:parse-ns(@ns) else ."/>
		<xsl:for-each select="if (@select) then f:eval($data, @select, $nc) else $data/*/*">
			<group id="{@id}" label="{if ($def/@label) then f:eval($def/@label, $nc) else @label}" xmlns="">
				<xsl:if test="$def/@link">
					<xsl:attribute name="link" select="f:eval($def/@link, $nc)"/>
				</xsl:if>
				<xsl:apply-templates select="$def/vz:line" mode="#current">
					<xsl:with-param name="data" select="."/>
				</xsl:apply-templates>
			</group>
		</xsl:for-each>
	</xsl:template>
	
	<xsl:template match="vz:line" mode="timeln_content">
		<xsl:param name="data"/>
		<xsl:variable name="def" select="."/>
		<xsl:variable name="nc" select="if (@ns) then f:parse-ns(@ns) else ."/>
		<xsl:for-each select="if (@select) then f:eval($data, @select, $nc) else $data/*">
			<line id="{@id}" label="{if ($def/@label) then f:eval($def/@label, $nc) else @label}" xmlns="">
				<xsl:variable name="v" select="f:eval($def/@start, $nc)"/>
				<xsl:if test="$v">
					<xsl:attribute name="start" select="$v"/>
				</xsl:if>
				<xsl:if test="$def/@end">
					<xsl:variable name="v" select="f:eval($def/@end, $nc)"/>
					<xsl:if test="$v">
						<xsl:attribute name="end" select="$v"/>
					</xsl:if>
				</xsl:if>
				<xsl:if test="$def/@link">
					<xsl:attribute name="link" select="f:eval($def/@link, $nc)"/>
				</xsl:if>
			</line>
		</xsl:for-each>
	</xsl:template>
	
	<xsl:template match="group" mode="viz_timeln">
		<tbody id="{@id}~1">
			<tr>
				<td>
					<xsl:apply-templates select="." mode="viz_timeln_label"/>
				</td>
				<td>
					&#160;
				</td>
			</tr>
			<xsl:apply-templates select="line" mode="#current"/>
		</tbody>
	</xsl:template>
	
	<xsl:template match="*" mode="viz_timeln_label">
		<xsl:value-of select="@label"/>
	</xsl:template>
	
	<xsl:template match="*[@link]" mode="viz_timeln_label">
		<a href="{f:ref-to-uri(@view, @link, '.html')}">
			<xsl:value-of select="@label"/>
		</a>
	</xsl:template>
	
	<xsl:template match="line" mode="viz_timeln">
		<xsl:param name="first" tunnel="yes" as="xs:date"/>
		<tr id="{@id}~1">
			<td>
				<xsl:text>&#160;&#160;</xsl:text>
				<xsl:apply-templates select="." mode="viz_timeln_label"/>
			</td>
			<td>
				<xsl:choose>
					<xsl:when test="@end">
						<xsl:variable name="s" select="days-from-duration(xs:date(@start) - $first) + day-from-date($first)"/>
						<xsl:variable name="d" select="days-from-duration(xs:date(@end) - xs:date(@start))"/>
						<xsl:choose>
							<xsl:when test="$d &gt; 70">
								<div class="bar" style="margin-left: {$s}px; width: {$d}px;">
									<div class="end">
										<xsl:value-of select="@end"/>
									</div>
								</div>
							</xsl:when>
							<xsl:otherwise>
								<div style="margin-left: {$s}px; width: {$s + $d + 40}px;">
									<div class="bar" style="width:{$d}px; margin-right: 1px;">&#160;</div>
									<div class="endOut">
										<xsl:value-of select="@end"/>
									</div>
								</div>
							</xsl:otherwise>
						</xsl:choose>
					</xsl:when>
					<xsl:otherwise>
						<xsl:text>&#160;</xsl:text>
					</xsl:otherwise>
				</xsl:choose>
			</td>
		</tr>
	</xsl:template>
	
</xsl:stylesheet>
